package com.example.chandao.common.model;

import cn.hutool.core.collection.CollUtil;
import com.example.chandao.common.enums.ErrCode;
import com.github.pagehelper.PageInfo;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data

public class ApiResult implements Serializable {

    private Integer code;

    private String msg;

    private Object data;


    private ApiResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private ApiResult(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public static ApiResult success() {
        return new ApiResult(ErrCode.SUCCESS.getCode(), ErrCode.SUCCESS.getMsg());
    }


    public static ApiResult success(Object data) {
        return new ApiResult(ErrCode.SUCCESS.getCode(), ErrCode.SUCCESS.getMsg(), data);
    }

    public static ApiResult page(List<?> dataList) {
        dataList = CollUtil.defaultIfEmpty(dataList, CollUtil.newArrayList());
        PageRest pageRest = new PageRest(new PageInfo<>(dataList).getTotal(), dataList);
        return new ApiResult(ErrCode.SUCCESS.getCode(), ErrCode.SUCCESS.getMsg(), pageRest);
    }

    public static ApiResult warn(String msg) {
        return new ApiResult(ErrCode.WARNING.getCode(), msg);
    }

    public static ApiResult error(String msg) {
        return new ApiResult(ErrCode.SYS_ERROR.getCode(), msg);
    }

    public static ApiResult error(Integer code, String msg) {
        return new ApiResult(code, msg);
    }

    public static ApiResult error(Integer code, String msg, Object data) {
        return new ApiResult(code, msg, data);
    }

}
